<template>

	<view class="d-login">

		<view class="d-login-btn">
			<template v-if="step == 2">
				<u-button class="d-btn" type="success" shape="circle" open-type="getUserInfo" @getuserinfo="getuserinfo"
					>微信授权</u-button>
			</template>
			<template v-if="step == 1">
				<u-button class="d-btn" type="primary" shape="circle" open-type="getPhoneNumber"
					@getphonenumber="getphonenumber" :custom-style="customStyle">授权手机号登入</u-button>
			</template>
		</view>
		<u-toast ref="uToast" />
		<view class="d-login-btn1">
			<u-button class="d-btn" type="warning" @click="back()" shape="circle">暂不登录</u-button>
		</view>
	</view>
</template>

<script>
	const obj = {
		0: {
			lable: '工人',
			value: 'any'
		},
		1: {
			label: '普通小中介',
			value: 'small'
		},
		2: {
			label: '专属小中介',
			value: 'small'
		},
		3: {
			label: '大中介',
			value: 'big'
		}
	};
	export default {
		data() {
			return {
				customStyle: {
					background: '#5650ee'
				},
				step: 1,
				type: 1, //普通小中介
				params: {
					sex: '',
					openId: '',
					sessionKey: '',
					encryptedData: '',
					iv: '',
					hidPic: '',
					realName: '',
					recId: '',
					rId: '',
					code: '',
					type: 3
				}
			};
		},
		onLoad(e) {

			if (e && e.type) {
				this.params.type = e.type;
			}
			var that = this;
			// this.form.rId = uni.getStorageSync('recId');
			this.recId = uni.getStorageSync('recId');
			uni.getStorage({
				key: 'wxUserInfo',
				success: function(res) {
					that.step = 1
				},
				fail: res => {
					that.step = 2
				}

			});

			uni.login({
				provider: 'weixin',
				success: loginRes => {
					// #ifdef MP-WEIXIN
					wx.getSetting({
						success: res => {
							console.log(res)
							wx.getUserInfo({
								success: res => {
									console.log(res)
									// 可以将 res 发送给后台解码出 unionId 
									uni.setStorageSync('wxUserInfo', res.userInfo);
								}
							})
						},
					})

					//#endif
					this.$u.api
						.postGetWechatOpenId({
							code: loginRes.code,
							type: 3
						})
						.then(res => {
							console.log(res, '[登录页面-获取微信openid]');
							this.params.openId = res.data.openid;
							this.params.sessionKey = res.data.session_key;
							uni.setStorageSync('openId', this.params.openId);
							// if (!!res.data.phone) {
							// 	// 登录
							// 	this.postLoginCheck();
							// } else {
							// 	this.step = 1;
							// 	uni.showToast({
							// 		title: '请授权手机号',
							// 		icon: 'none'
							// 	})
							// }
						})
						.catch(err => {
							console.log(err);
						});
				}
			});
		},
		methods: {
			back() {
				uni.reLaunch({
					url: 'index/index'
				})
			},
			// 判断用户是否登录
			postIsLogin() {
				this.$u.api
					.postIsLogin()
					.then(res => {
						if (res.code == 1000) {
							console.log(res, '[登录页面-判断用户是否登录]');
							// 获取用户信息
							this.postGetAdminInfo();
							uni.reLaunch({
								url: './index/home'
							});
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 获取微信用户信息
			getuserinfo(e) {
				var that = this
				// this.params.sex = e.detail.userInfo.gender;
				// this.params.hidPic = e.detail.userInfo.avatarUrl;
				// this.params.realName = e.detail.userInfo.nickName;
				// 获取微信openid
				wx.getSetting({
					success: res => {
						console.log(res)
						wx.getUserInfo({
							success: res => {
								console.log(res)
								// 可以将 res 发送给后台解码出 unionId
								that.params.sex = res.userInfo.gender;
								that.params.hidPic = res.userInfo.avatarUrl;
								that.params.realName = res.userInfo.nickName;
								uni.setStorageSync('wxUserInfo', res.userInfo);
							}
						})
					},
				})
				this.postGetWechatOpenId();
			},
			// 获取微信openid
			postGetWechatOpenId() {
				
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						this.$u.api
							.postGetWechatOpenId({
								code: loginRes.code,
								type: 3
							})
							.then(res => {
								console.log(res, '[登录页面-获取微信openid]');
								this.params.openId = res.data.openid;
								this.params.sessionKey = res.data.session_key;
								uni.setStorageSync('openId', this.params.openId);
								if (!!res.data.phone) {
									// 登录
									this.postLoginCheck();
								} else {
									this.step = 1;
									uni.showToast({
										title: '请点击手机号登入',
										icon: 'none'
									});
								}
							})
							.catch(err => {
								console.log(err);
							});
					}
				});
			},
			// 授权手机号
			getphonenumber(e) {
				console.log(e.phoneNumber);
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					this.params.encryptedData = e.detail.encryptedData;
					this.params.iv = e.detail.iv;
					this.params.recId = uni.getStorageSync('recId');
					this.params.rId = uni.getStorageSync('recId');
					this.params.type = 3;
					// 登录
					this.postLoginCheck();
				} else {
					uni.alert('若拒绝无法体验更多功能');
				}
			},
			// 登录
			postLoginCheck() {
				let params = {
					...this.params
				};
				this.$u.api
					.postLoginCheck(params)
					.then(res => {
						if (res.status == true) {
							console.log(res, '[登录页面-登录]');
							uni.setStorageSync('token', res.data);
							// 获取用户信息
							this.postGetAdminInfo().then(res => {

								// uni.navigateBack()

							});
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 获取用户信息
			postGetAdminInfo() {
				return new Promise((resolve, reject) => {
					this.$u.api
						.postGetAdminInfo()
						.then(res => {
							console.log(res)
							if (res.status == true) {
								uni.setStorageSync('userPhone', res.data.phone);
								// this.$store.commit('commitUserId', res.data.id);
								uni.setStorageSync('userId', res.data.id);
								uni.setStorageSync('userInfo', res.data);
								this.$store.commit('commitUserInfo', res.data);
								switch (res.data.type) {
									case 0:
										uni.setStorageSync('role', 'any');
										uni.setStorageSync('roleType', 0);
										break;
									case 1:
										uni.setStorageSync('role', 'small');
										uni.setStorageSync('roleType', 1);
										break;
									case 2:
										uni.setStorageSync('role', 'small');
										uni.setStorageSync('roleType', 2);
									case 3:
										uni.setStorageSync('role', 'big');
										uni.setStorageSync('roleType', 3);
								}
							}
							if (res && res.data.applyType) {
								uni.reLaunch({
									url: 'apply'
								})
							} else {
								uni.reLaunch({
									url: './index/index'
								});
							}
							resolve();
						})
						.catch(err => {
							console.log(err);
							reject();
						});
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.d-login {
		position: fixed;
		top: 0;
		width: 100%;
		height: 100%;
		background: url() no-repeat left top;
		background-size: 100% auto;
		background-color: #fff;

		.d-login-btn {
			position: fixed;
			bottom: 170rpx;
			left: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;

			.d-btn {
				width: 690rpx;
			}
		}

		.d-login-btn1 {
			position: fixed;
			bottom: 60rpx;
			left: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;

			.d-btn {
				width: 690rpx;
			}
		}
	}
</style>
